package com.ingrid.javi.activities;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.ingrid.javi.R;
import com.ingrid.javi.R.id;
import com.ingrid.javi.R.layout;
import com.ingrid.javi.R.string;
import com.ingrid.javi.activities.validation.FormFilledListener;
import com.ingrid.javi.model.DAO;
import com.ingrid.javi.model.EditTextValidator;
import com.ingrid.javi.model.ValueInRangeValidator;
import com.ingrid.javi.util.AlertController;

public class ColocaNotaActivity extends Activity {

	static final String EXTRA_SELECTED_ID = "extraSelectedId";
	private EditText etNota;
	private long movieId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.nota);
		this.movieId = this.getIntent().getLongExtra(EXTRA_SELECTED_ID, -1);

		this.etNota = (EditText) findViewById(R.id.etNota);

		Button btOK = (Button) findViewById(R.id.btOK);
		new FormFilledListener(new EditTextValidator[]{new ValueInRangeValidator(etNota, 0, 10)}, btOK);

		btOK.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onClickModifyMovie();
			}

		});

	}

	private void onClickModifyMovie() {
		String nota = this.etNota.getText().toString();
		DAO dao = new DAO(this);
		dao.markWish(this.movieId, nota);

		AlertController.showSuccessMessage(this,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						ColocaNotaActivity.this.finish();
						onFinishActivity();
					}

				}, this.getString(R.string.modify_grade));
	}

	private void onFinishActivity() {
		Intent intent = new Intent(getApplicationContext(), WishListActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	}

}
